package com.glory.nicleanrpt

import com.glory.njcleanrpt.DataUtil
import com.glory.njcleanrpt.DateUtil
import com.glory.njcleanrpt.TranposUtils
import com.ivan.utils.PathUtil

/**
 * @Program: njcleanrpt
 * @Description: 首个报表
 * @Author: ivan
 */
class FirstReportController {

    def dynamicQueryService

    /**
     * 一阶统计
     * @Author Ivan
     * @Date 2019/6/27
     */
    def firstReport() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.startTime = DateUtil.getStartDate(params.startTime).startDate
        params.endTime = DateUtil.getEndDate(params.endTime).endDate
        def dateList = []
        def report_011 = []
        def tableHeadList = []
        def yieldByDayChart = []
        def reworkYieldByDayChart = []
        def scrapYieldByDayChart = []
        def sysQueryPath = PathUtil.getPath(this, request)
        List<Integer,Double> qtyList = new ArrayList<>()
        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            // 日期
            params.SYS_QUERY_NAME = 'dateList'
            dateList = dynamicQueryService.queryForList(params)
            // 各工段(工步)进出站List
            params.SYS_QUERY_NAME = 'firstReportList'
            report_011 = dynamicQueryService.queryForList(params)
            if (report_011.size() > 0) {
                tableHeadList = report_011[0].keySet()
            }
            def waitTotalQty = 0
            def wipTotalQty = 0
            def inTotalQty = 0
            def runTotalQty = 0
            def outTotalQty = 0
            Double yieldTotal = 0
            def holdTotalQty = 0
            def reworkTotalQty = 0
            Double reworkYieldTotal = 0
            def scrapTotalQty = 0
            Double scrapYieldTotal = 0
            def rptSize = report_011.size()
            for (int i = 0; i < rptSize; i++) {
                waitTotalQty += report_011[i].wait_qty
                wipTotalQty += report_011[i].wip_qty
                inTotalQty += report_011[i].in_qty
                runTotalQty += report_011[i].run_qty
                outTotalQty += report_011[i].out_qty
                holdTotalQty += report_011[i].hold_qty
                reworkTotalQty += report_011[i].rework_qty
                scrapTotalQty += report_011[i].scrap_qty
            }
            yieldTotal = DataUtil.saveOneBitTwoRound(outTotalQty/(waitTotalQty+inTotalQty)*100)
            reworkYieldTotal = DataUtil.saveOneBitTwoRound(reworkTotalQty/(waitTotalQty+inTotalQty)*100)
            scrapYieldTotal = DataUtil.saveOneBitTwoRound(scrapTotalQty/(waitTotalQty+inTotalQty)*100)
            qtyList.add(0, waitTotalQty)
            qtyList.add(1, wipTotalQty)
            qtyList.add(2, inTotalQty)
            qtyList.add(3, runTotalQty)
            qtyList.add(4, outTotalQty)
            qtyList.add(5, yieldTotal)
            qtyList.add(6, holdTotalQty)
            qtyList.add(7, reworkTotalQty)
            qtyList.add(8, reworkYieldTotal)
            qtyList.add(9, scrapTotalQty)
            qtyList.add(10, scrapYieldTotal)
            // 每日比率
            params.SYS_QUERY_NAME = 'firstReportYieldByDayChart'
            yieldByDayChart = dynamicQueryService.queryForList(params)
            yieldByDayChart = TranposUtils.colToRow('GD_STEP_DESC', 'YIELD', yieldByDayChart)
            // 每日返工率
            params.SYS_QUERY_NAME = 'firstReportReworkYieldByDayChart'
            reworkYieldByDayChart = dynamicQueryService.queryForList(params)
            reworkYieldByDayChart = TranposUtils.colToRow('GD_STEP_DESC', 'REWORK_YIELD', reworkYieldByDayChart)
            // 每日报废率
            params.SYS_QUERY_NAME = 'firstReportScrapYieldByDayChart'
            scrapYieldByDayChart = dynamicQueryService.queryForList(params)
            scrapYieldByDayChart = TranposUtils.colToRow('GD_STEP_DESC', 'SCRAP_YIELD', scrapYieldByDayChart)
        }
        [
                startTime            : params.startTime ?: '',
                endTime              : params.endTime ?: '',
                report_011           : report_011,
                tableHeadList        : tableHeadList,
                qtyList              : qtyList,
                dateList             : dateList,
                yieldByDayChart      : yieldByDayChart,
                reworkYieldByDayChart: reworkYieldByDayChart,
                scrapYieldByDayChart : scrapYieldByDayChart,
                SYS_QUERY_PATH       : sysQueryPath,
                SYS_QUERY_NAME       : 'firstReportList'
        ]
    }

    /**
     * 二阶明细
     * @Author Ivan
     * @Date 2019/6/27
     */
    def firstReportDetail() {
        params.dgStepDesc = URLDecoder.decode(params.dgStepDesc, 'UTF-8')
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.startTime = DateUtil.getStartDate(params.startTime).startDate
        params.endTime = DateUtil.getEndDate(params.endTime).endDate
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        def reportList = []
        def tableHeadList = []
        def totalCount = 0
        def sysQueryName
        if ('WAIT_QTY'.equals(params.clazz)) {
            // 等待
            params.SYS_QUERY_NAME = 'firstReportDetailByWaitList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'firstReportDetailByWaitCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('WIP_QTY'.equals(params.clazz)) {
            // 在制
            params.SYS_QUERY_NAME = 'firstReportDetailByWipList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'firstReportDetailByWipCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('IN_QTY'.equals(params.clazz)){
            // 进站
            params.SYS_QUERY_NAME = 'firstReportDetailByInList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'firstReportDetailByInCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('RUN_QTY'.equals(params.clazz)){
            // 站内
            params.SYS_QUERY_NAME = 'firstReportDetailByRunList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'firstReportDetailByRunCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('OUT_QTY'.equals(params.clazz)){
            // 出站
            params.SYS_QUERY_NAME = 'firstReportDetailByOutList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'firstReportDetailByOutCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('HOLD_QTY'.equals(params.clazz)) {
            // HOLD
            params.SYS_QUERY_NAME = 'firstReportDetailByHoldList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'firstReportDetailByHoldCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('REWORK_QTY'.equals(params.clazz)) {
            // 返工
            params.SYS_QUERY_NAME = 'firstReportDetailByReworkList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'firstReportDetailByReworkCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('SCRAP_QTY'.equals(params.clazz)) {
            // 报废
            params.SYS_QUERY_NAME = 'firstReportDetailByScrapList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'firstReportDetailByScrapCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        }
        [
                pageCurrent   : pageCurrent,
                pageSize      : pageSize,
                pageSizeList  : pageSizeList,
                startno       : params.startno ?: '',
                endno         : params.endno ?: '',
                startTime     : params.startTime ?: '',
                endTime       : params.endTime ?: '',
                dgStepName    : params.dgStepName ?: '',
                dgStepDesc    : params.dgStepDesc ?: '',
                clazz         : params.clazz ?: '',
                tableHeadList : tableHeadList,
                reportList    : reportList,
                totalCount    : totalCount,
                SYS_QUERY_NAME: sysQueryName
        ]
    }

    def exportExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = 'firstReportList'
        def report_011 = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (report_011.size() > 0) {
            tableHeadList = report_011[0].keySet()
        }
        def waitTotalQty = 0
        def wipTotalQty = 0
        def inTotalQty = 0
        def runTotalQty = 0
        def outTotalQty = 0
        Double yieldTotal = 0
        def holdTotalQty = 0
        def reworkTotalQty = 0
        Double reworkYieldTotal = 0
        def scrapTotalQty = 0
        Double scrapYieldTotal = 0
        List<Integer,Double> qtyList = new ArrayList<>()
        def rptSize = report_011.size()
        for (int i = 0; i < rptSize; i++) {
            waitTotalQty += report_011[i].wait_qty
            wipTotalQty += report_011[i].wip_qty
            inTotalQty += report_011[i].in_qty
            runTotalQty += report_011[i].run_qty
            outTotalQty += report_011[i].out_qty
            holdTotalQty += report_011[i].hold_qty
            reworkTotalQty += report_011[i].rework_qty
            scrapTotalQty += report_011[i].scrap_qty
        }
        yieldTotal = DataUtil.saveOneBitTwoRound(outTotalQty/inTotalQty*100)
        reworkYieldTotal = DataUtil.saveOneBitTwoRound(reworkTotalQty/inTotalQty*100)
        scrapYieldTotal = DataUtil.saveOneBitTwoRound(scrapTotalQty/inTotalQty*100)
        qtyList.add(0, waitTotalQty)
        qtyList.add(1, wipTotalQty)
        qtyList.add(2, inTotalQty)
        qtyList.add(3, runTotalQty)
        qtyList.add(4, outTotalQty)
        qtyList.add(5, yieldTotal)
        qtyList.add(6, holdTotalQty)
        qtyList.add(7, reworkTotalQty)
        qtyList.add(8, reworkYieldTotal)
        qtyList.add(9, scrapTotalQty)
        qtyList.add(10, scrapYieldTotal)
        String filename = new String("firstReportList.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [qtyList: qtyList, dataList: report_011, tableHeadList: tableHeadList])
    }
}